package com.table;

import java.util.ArrayList;
import java.util.List;

import tools.XMLParser;
import world.World;

import com.element.Element;
import com.element.ElementList;

public class Table extends ArrayList<TableElement> {

	/* Name of id must be same as name of xml-file + number, for example: itemtable0, enemytable23 */
	public Table(String id, World world){
		XMLParser parser = new XMLParser();
		parser.setTable(id, this, world);
	}
	
	/* returns a list with TableElements */
	ElementList makeList(){
		ElementList elementList = new ElementList();
		int idx = 0;

		/* make the returned list. */ 
		while(idx < this.size()){
			boolean onlyLetThisOccur = false;

			/* If the occurance size is too large, then use another algorithm 
			 * to calculate number of occurances */
			TableElement te = this.get(idx);

			if(te.isEnabled()){
				if(te.isOnlyLetThisOccur()){
					elementList.clear();
					onlyLetThisOccur = true;
				}

				if(te.getSize() <= TableElement.OCCUR_SIZE_LIMIT || TableElement.OCCUR_SIZE_LIMIT == -1){

					/* "Iterate" through size */
					int idx2 = te.getSize();
					while(idx2 > 0){

						if(tools.Math.randomDouble(0,1) <= te.getChance()){
							/* Occurance happened. 
							 * Clone the element and add it to the list! */
							Element e = te.getElement().clone();
							elementList.add(e);
						}

						idx2--;
					}
				} else {
					/* add mathematical algorithm here for huge "sizes" */
				}
			}

			if(onlyLetThisOccur){
				return elementList;
			}
			idx++;
		}
		return elementList;
	}
}
